package com.horus.entities.dao.hibernate;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.horus.entities.dto.user.User;

public class UserDao extends HibernateDao{

	

	public UserDao() {
		
		sessionFactory = HibernateFactory.getSessionFactory();
		session = sessionFactory.openSession();
		this.transaction = session.beginTransaction();
	}

	public void saveUser(User user) {

		try {
			session.save(user);
			transaction.commit();

		} catch (HibernateException he) {
			transaction.rollback();
			throw new HibernateException(he);
		}

		session.close();
	}

	public User getUser(long id) {

		User user = null;

		try {
			user = (User) session.get(User.class, id);
			transaction.commit();

		} catch (HibernateException he) {
			transaction.rollback();
			throw new HibernateException(he);
		}

		session.close();

		return user;
	}

	public User getTransactedUser(long id) {

		User user = null;

		try {
			user = (User) session.get(User.class, id);

		} catch (HibernateException he) {
			throw new HibernateException(he);
		}

		return user;
	}

	public boolean deleteUser(int id) {

		User user = (User) session.get(User.class, id);
		return deleteUser(user);
	}

	public boolean deleteUser(User user) {

		Session session = sessionFactory.openSession();
		Transaction transaction = session.beginTransaction();

		user.setActive(false);
		session.update(user);

		transaction.commit();
		session.close();

		return true;
	}


}
